package ae_server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import net.*; 

public class aeServerControl
{
	private static final int defaultCapacity=4, defaultPort=1255;
	private static TCP_MultiThreadServer server;
	private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	private static String temp = "", data[];
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		System.out.println("Server is ready to roll!");
		
		while(true)
		{
			try
			{
				temp = in.readLine();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			data = temp.split(" ");
			if(data[0].equals("start"))
			{
				if(server!=null)
				{
					System.out.println("Server is running");
					continue;
				}
				server = new TCP_MultiThreadServer();
				switch(data.length)
				{
				case 1:
					server.start(defaultPort, defaultCapacity);
					break;
				case 2:
					server.start(Integer.parseInt(data[1]), defaultCapacity);
					break;
				case 3:
					server.start(Integer.parseInt(data[1]), Integer.parseInt(data[2]));
					break;
				}
			}
			else if(data[0].equals("stop"))
			{
				if(server==null)
				{
					System.out.println("Server is not running");
					continue;
				}
				server.shutdown();
				server=null;
			}
			else if(data[0].equals("exit") || data[0].equals("shutdown") || data[0].equals("quit"))
			{
				System.out.println("Forcing exit...");
				System.exit(0);
			}
			else
				System.out.println("Unknown command: " + temp);
		}
	}

}
